import store from '@/store'

export default {
  // 指令名:{生命周期}
  imagerror: {
    inserted (dom, obj, vnode) {
      dom.src = dom.src || obj.value
      dom.onerror = function () {
        dom.src = obj.value
      }
    },
    componentUpdated (dom, obj) {
      dom.src = dom.src || obj.value
    },
    unbind (dom, obj) {
      dom.onerror = null
    }
  },
  test: {},
  //   点击dom以外执行一个回调方法
  clickoutside: {
    inserted (dom, obj, vnode) {
      const fn = e => {
        if (!dom.contains(e.target)) {
          const callback = obj.value
          callback()
        }
      }
      vnode.fn = fn
      document.addEventListener('click', fn)
    },
    unbind (dom, obj, vnode) {
      document.removeEventListener('click', vnode.fn)
    }
  },
  // 控制权限控制指令
  control: {
    inserted (dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    },
    componentUpdated (dom, obj) {
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    }
  }
}
